/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mobiwebinc.compconn.events;

import com.mobiwebinc.compconn.communication.IOHandler;
import com.mobiwebinc.compconn.configuration.ServerConfiguration;
import com.mobiwebinc.compconn.events.command.Command;
import com.mobiwebinc.compconn.events.command.Decoder;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author suraj
 */
public class CopyPaste implements EventHandler, Transferable, ClipboardOwner {

    Clipboard clipboard;
    IOHandler io;
    String pasteContent;

    public CopyPaste(IOHandler io) {
        this.io = io;
        clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    }

    public void handleRequest(String cmd) {
//        System.out.println("cmd: " + cmd);
        if (cmd.startsWith(Command.PASTE_TEXT)) {
            pasteText(cmd);
        } else if (cmd.startsWith(Command.COPY_TEXT)) {
            copyText();
        }
    }

    private void pasteText(String cmd) {
        pasteContent = Decoder.stringParameter(cmd);
        clipboard.setContents(this, this);
        system.keyPress(ServerConfiguration.getInstance().commandKey());
        system.keyPress(KeyEvent.VK_V);
        system.keyRelease(KeyEvent.VK_V);
        system.keyRelease(ServerConfiguration.getInstance().commandKey());
    }

    private void copyText() {
        system.keyPress(ServerConfiguration.getInstance().commandKey());
        system.keyPress(KeyEvent.VK_C);
        system.keyRelease(KeyEvent.VK_C);
        system.keyRelease(ServerConfiguration.getInstance().commandKey());
        try {
            Thread.sleep(400);
        } catch (InterruptedException ex) {
            Logger.getLogger(CopyPaste.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            Transferable content = clipboard.getContents(this);
            if (content.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                System.out.println("content: " + content.getTransferData(DataFlavor.stringFlavor));
                io.sendMsg(Command.COPIED_CONTENT + " " + content.getTransferData(DataFlavor.stringFlavor));
            }
        } catch (Exception ex) {
            Logger.getLogger(CopyPaste.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public DataFlavor[] getTransferDataFlavors() {
        DataFlavor[] flavor = {DataFlavor.imageFlavor, DataFlavor.stringFlavor};
        return flavor;
    }

    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return flavor.equals(DataFlavor.imageFlavor) || flavor.equals(DataFlavor.stringFlavor);
    }

    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
        if (flavor.equals(DataFlavor.stringFlavor)) {
            return pasteContent;
        } else if (flavor.equals(DataFlavor.imageFlavor)) {
        } else {
            throw new UnsupportedFlavorException(flavor);
        }
        return null;
    }

    public void lostOwnership(Clipboard clipboard, Transferable contents) {
        pasteContent = "";
    }

    public boolean isSupported(String cmd) {
        return cmd.startsWith(Command.COPY_PASTE_PREFIX);
    }
}
